// pages/service_date_manager/service_date_manager.js
const config = require('../../config.js');
const utils = require('../../utils/util.js');
const request = require('../../utils/request.js')
import {
    getAppointDate,
    getAppointTime,
    setAppointTime
} from '../../static/api/api.js'
let app = new getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        nowDate:null,
        nowDateData:{
            year:'',
            month:'',
            day:''
        },
        selectAll:false,
        dateTime:null,
        curDateTime:null,
        appointTime:null,
        appointTimeStatus:null,
        pageLoad:false,
        defData:null
        
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            'appointTimeStatus': utils.appointTimeStatus
        })
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getAppointDate()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
    //判断是否有修改
    isChangeData(){
        let result;
        for(var i in this.data.appointTime){
            if (this.data.appointTime[i] != this.data.defData[i]){
                return true
            }
        }
        return false
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    switchChange(e){
        let that = this;
        let text = '';
        if (!that.data.selectAll){
            text ="确定要设置当天所有时间可预约吗？";
        }else{
            text = "确定要关闭当天所有可预约时间吗？";
        }
        wx.showModal({
            title: '温馨提示',
            content: text,
            success: function (res) {
                if (res.confirm) {
                    
                    let c = that.data.appointTime;
                    for(var i in c){
                        if(c[i]!=0 && c[i]!=4){
                            if (!that.data.selectAll){
                                c[i] = 2;
                            }else{
                                c[i] = 1;
                            }
                            
                        }
                        
                    }
                    that.setData({
                        appointTime:c,
                        'selectAll': e.detail.value
                    })

                } else if (res.cancel) {
                    that.setData({
                        'selectAll': that.data.selectAll
                    })
                }

            }
        })
        
    },
    isAllSelected(){
        let len = 0;
        let slen = 0;
        for (var i in this.data.appointTime){
            if (this.data.appointTime[i] != 0 && this.data.appointTime[i]!=4){
                len++;
                if (this.data.appointTime[i]==2){
                    slen++;
                }
               
            }
        }
        //console.log(len,slen)
        return len == slen
    },
    setAppointTime(){
        let is_close = [], is_open=[];
        for (var i in this.data.appointTime){
            if (this.data.appointTime[i] == 1) {
                is_close.push(i);
            }
            if (this.data.appointTime[i] == 2) {
                is_open.push(i);
            }
        }
        let params={
            set_date:this.data.curDateTime.date,
            is_open: is_open.join(','),
            is_close: is_close.join(',')
        }
        wx.showLoading({
            title: '加载中...',
        })
        setAppointTime(params).then(response => {
            wx.hideLoading()
            wx.showToast({
                icon:'none',
                title:'保存成功'
            })
            this.setData({
                'defData': this.data.appointTime
            })
        }).catch(response => {
            wx.hideLoading()
            //console.log(response)
        })
    },
    getAppointDate() {
        wx.showLoading({
            title: '加载中...',
        })
        getAppointDate().then(response => {
            wx.hideLoading()
            this.setData({
                'dateTime': response.data.date_list,
                'curDateTime': response.data.date_list[0],
                'appointTime': response.data.appoint_time.time_data,
                'defData': Object.assign({}, response.data.appoint_time.time_data)
            })
            let cDate = new Date(response.data.date_list[0].date.replace('-', '/'))
            
            this.setData({
                'nowDateData.year':cDate.getFullYear(),
                'nowDateData.month': cDate.getMonth(),
                'nowDateData.day': cDate.getDate(),
            })
            let fg = this.isAllSelected()
            this.setData({
                'selectAll': fg
            })
        }).catch(response => {
            wx.hideLoading()
            //console.log('数据获取失败')
            //console.log(response)
        })
    },
    changeDate(e){
        let that = this;
        let r = this.isChangeData();
        if(r){
            wx.showModal({
                title: '温馨提示',
                content: '您确定要放弃当前修改吗？',
                success(res) {
                    if (res.confirm) {
                        that.getAppointTime(e)
                    } else if (res.cancel) {
                       // console.log('用户点击取消')
                    }
                }
            })
        }else{
            that.getAppointTime(e)
        }
        
    },
    getAppointTime(e) {
        let dataset = e.currentTarget.dataset;
        let params = {
            'set_date': dataset.item.date,
            
        };
        this.setData({
            pageLoad:true,
            'appointTime': null,
            'curDateTime': dataset.item
        })
        getAppointTime(params).then(response => {
            
            
            this.setData({
                pageLoad: false,
                'appointTime': response.data.time_data,
                'defData': Object.assign({}, response.data.time_data)
            })
            let fg = this.isAllSelected()
            this.setData({
                'selectAll': fg
            })
        }).catch(response => {
            this.setData({
                pageLoad: false
            })
            //console.log('数据获取失败')
            //console.log(response)
        })
    },
    appointTimeItemCb(e){
        let dataset = e.currentTarget.dataset;
        if (dataset.val == 0 || dataset.val == 4){
            return;
        }
        let key = 'appointTime.'+dataset.key+''
        //console.log(dataset.key);
        this.setData({
            [key]: (dataset.val==1)?"2":"1"
        })
        let fg = this.isAllSelected()
        //console.log(fg)
        this.setData({
            'selectAll': fg
        })
    }
})